package qsbk.app.play.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import qsbk.app.play.common.TopicPool;
import qsbk.app.play.model.Status;
import qsbk.app.play.servlet.response.StatusesTimelineResponse;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/statuses/timeline")
public class StatusServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static final Gson gson = new Gson();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StatusServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());

		TopicPool topicTool = new TopicPool();

		String topic = "乒乓球";

		Status status = new Status();
		status.pic = "http://ocekwf7fx.bkt.clouddn.com/%E4%B9%92%E4%B9%93%E7%90%83%E5%B0%81%E9%9D%A2.png";
		status.url = "http://ocekwf7fx.bkt.clouddn.com/%E4%B9%92%E4%B9%93%E7%90%83.mp4";
		status.topic = topic;
		status.id = 1;
		status.words = topicTool.generateGuessWords(topic);

		List<Status> statuses = new ArrayList<>();
		statuses.add(status);

		StatusesTimelineResponse result = new StatusesTimelineResponse();
		result.statuses = statuses;

		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		writer.append(gson.toJson(result));
		writer.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
